package cn.cq.admin.controller;

import cn.cq.admin.entity.vo.UserVo;
import cn.cq.annotation.OperationLogAnnotation;
import cn.cq.admin.entity.dto.BatchRemove;
import cn.cq.admin.entity.dto.PasswordResetDto;
import cn.cq.admin.entity.dto.UserLoginDto;
import cn.cq.admin.entity.dto.UserModifyDto;
import cn.cq.admin.entity.dto.UserPageDto;
import cn.cq.admin.entity.model.TUser;
import cn.cq.admin.service.TUserService;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * cn.cq.admin.controller
 *
 * @author  dicraft
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private TUserService userService;

    /**
     * 账号密码登录
     * @param loginDto
     * @return
     */
    @PostMapping("/doLogin")
    public SaResult accountLogin(@RequestBody UserLoginDto loginDto, HttpServletRequest request){
        return userService.accountLogin(loginDto,request);
    }

    /**
     * 用户登出
     * @return
     */
    @OperationLogAnnotation(module = "用户管理",type = "登出",description = "用户登出")
    @SaCheckLogin
    @GetMapping("/doLogout")
    public SaResult logout(){
        StpUtil.logout();
        return SaResult.ok();
    }

    /**
     * 检查登录用户是否登录，如果登录，则返回用户资源
     */
    @OperationLogAnnotation(module = "用户管理",type = "查询",description = "检查登录用户是否登录，如果登录，则返回用户资源")
    @SaCheckLogin
    @GetMapping("/doCheckLogin")
    public SaResult doCheckLogin(){
        return userService.doCheckLogin();
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @OperationLogAnnotation(module = "用户管理",type = "新增",description = "新增用户")
    @SaCheckPermission(value = "Settings-User-Add", orRole = "SUPER-ADMIN")
    @PostMapping("/doSave")
    public SaResult doSave(@RequestBody TUser user){
        return userService.doSave(user);
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @OperationLogAnnotation(module = "用户管理",type = "修改",description = "修改用户")
    @SaCheckPermission(value = "Settings-User-Modify", orRole = "SUPER-ADMIN")
    @PutMapping("/doModify")
    public SaResult doModify(@RequestBody UserModifyDto user){
        return userService.doModify(user);
    }

    /**
     * 删除用户
     * @param batchRemove
     * @return
     */
    @OperationLogAnnotation(module = "用户管理",type = "删除",description = "删除用户")
    @SaCheckPermission(value = "Settings-User-Delete", orRole = "SUPER-ADMIN")
    @DeleteMapping("/doRemove")
    public SaResult doRemove(@RequestBody BatchRemove batchRemove){
        return userService.doRemove(batchRemove.getIds());
    }

    /**
     * 用户分页列表
     * @param userPageDto
     * @return
     */
    @OperationLogAnnotation(module = "用户管理",type = "查询",description = "用户分页列表")
    @SaCheckPermission(value = "Settings-User-Query", orRole = "SUPER-ADMIN")
    @PostMapping("/doList")
    public SaResult doList(@RequestBody UserPageDto userPageDto){
        return userService.doList(userPageDto);
    }

    /**
     * 获取登录用户信息
     * @return
     */
    @OperationLogAnnotation(module = "用户管理",type = "查询",description = "获取登录用户信息")
    @SaCheckLogin
    @GetMapping("/doGet")
    public SaResult doGet(){
        return userService.doGet();
    }

    /**
     * 修改密码
     * @param passwordResetDto
     * @return
     */
    @OperationLogAnnotation(module = "用户管理",type = "修改",description = "修改密码")
    @SaCheckLogin
    @PostMapping("/doResetPassword")
    public SaResult doPasswordReset(@RequestBody PasswordResetDto passwordResetDto){
        return userService.doPasswordReset(passwordResetDto);
    }


    @GetMapping("/doListAll")
    public SaResult doListAll() {
        List<UserVo> userVos = userService.doListAll();
        return SaResult.data(userVos);
    }
}
